package org.geektimes.projects.user.web.controller;

import org.geektimes.projects.user.domain.User;
import org.geektimes.projects.user.service.UserService;
import org.geektimes.web.mvc.controller.Controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import java.util.Collection;
import java.util.Objects;

/**
 * Router Page
 *
 * @author biwin
 */
@Path("/")
public class IndexController implements Controller {
    @Resource(name = "bean/UserService")
    private UserService userService;

    @Path("")
    @GET
    public String index() {
        return "index.jsp";
    }

    @GET
    @Path("/signUp")
    public String toRegister(HttpServletRequest request, HttpServletResponse response) {
        return "sign-up.jsp";
    }

    @GET
    @Path("/changeUser/{id}")
    public String toChangeUser(@PathParam("id") Long id, HttpServletRequest request, HttpServletResponse response) {
        User user = userService.queryUserById(id);
        request.setAttribute("user", user);
        return "change-user.jsp";
    }

    @GET
    @Path("/users")
    public String toUserList(HttpServletRequest request, HttpServletResponse response) {
        Collection<User> users = userService.getAll();
        if (Objects.nonNull(users) && !users.isEmpty()) {
            request.setAttribute("users", users);
        }
        return "users.jsp";
    }

    @GET
    @POST
    @Path("/showConfig")
    public String showConfigValue(HttpServletRequest request, HttpServletResponse response) {
        request.setAttribute("applicationName", userService.getConfigValue());
        return "showConfig.jsp";
    }
}
